﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using JKF.DataModule.SqlServerDAL;
using JKF.Substructure;
using JKF.Entities.Model;

namespace JKF.BusinessLayer.BLL
{
    public partial class bllSYS_USER
    {
        private const string _sessionUser = "__user";
        public static SYS_USERinfo CurrentUser
        {
            get
            {
                return SessionManager.Get(_sessionUser) as SYS_USERinfo;
            }
            private set
            {
                SessionManager.Set(_sessionUser, value);
            }
        }

        private const string userModulecachekey = "OrgUserModule";
        public static void InitUserSession(SYS_USERinfo user)
        {
            //SYS_USERinfo u = new SYS_USERinfo();

            CurrentUser = user;
            //SessionManager.Set(userModulecachekey, null);
        }

        public  bool LogIn(string username, string pwd)
        {
            
            var ds = dalSYS_USER.SysUserInfo(username, pwd);

            var l = DataTableToList(ds.Tables[0]);
            if (l.Count == 1)
            {
                InitUserSession(l[0]);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="ACCOUNT">账号</param>
        /// <param name="REAL_NAME">姓名</param>
        /// <param name="ROLE_KEY">角色ID</param>
        /// <param name="SHOP_CODE">店面编码</param>
        /// <param name="AVAILABLE">是否有效</param>
        /// <param name="pageCurrent">当前页码</param>
        /// <param name="pageSize">一页显示多少</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录数</param>
        /// <returns>返回DataTable数据</returns>
        public DataTable GetUsersBLL(SYS_USERinfo userInfo, string ACCOUNT, string REAL_NAME, string ROLE_KEY, string SHOP_CODE, string AVAILABLE, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            return dalSYS_USER.GetUsersDAL(userInfo, ACCOUNT, REAL_NAME, ROLE_KEY, SHOP_CODE, AVAILABLE, pageCurrent, pageSize, ref pageCount, ref rowCount);
        }
         /// <summary>
        /// 判断是否存在此名称,true表示存在
        /// </summary>
        /// <param name="shop_name">店面名称</param>
        /// <returns></returns>
        public bool ExistsUserAccountBLL(string user_account)
        {
            return dalSYS_USER.ExistsUserAccountDAL(user_account);
        }
        /// <summary>
        /// 采用事务的方式执行Sql语句
        /// </summary>
        public bool TransactionInsertORUpdateUser_ShopBLL(string sql)
        {
            return dalSYS_USER.TransactionInsertORUpdateUser_ShopDAL(sql);
        }

        /// <summary>
        /// 用户登出
        /// </summary>
        public static void LogOut()
        {
            SessionManager.Clear();
        }
        /// <summary>
        /// 删除账户,采用存储过程。先留着接口，后续再完善
        /// </summary>
        /// <param name="user_key"></param>
        public  string SysUserDeleteBLL(string user_key)
        {
            return dalSYS_USER.SysUserDeleteDAL(user_key);
        }
        /// <summary>
        /// 构建店铺和省树结构数据
        /// </summary> 
        /// <returns>返回List结果集</returns>
        public List<JKF.BusinessLayer.BLL.bllPRODUCT.PRODUCT_SORT_TREE> GetShopProvinceTreeBLL()
        {
            return new bllPRODUCT().DataTableToTreeList(dalSYS_USER.GetShopProvinceTreeDAL());
        }
    }
}
